package ru.compscicenter.uno.view;

import ru.compscicenter.uno.model.Player;
import ru.compscicenter.uno.util.ImagePool;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

/**
 * Created by IntelliJ IDEA.
 * User: Павел
 * Date: 31.03.12
 * Time: 9:33
 * To change this template use File | Settings | File Templates.
 */
public class PlayerPanel extends JPanel {
    private static BufferedImage unactiveImage;
    private static BufferedImage activeImage;
    private BufferedImage avatar;
    private ImageButton button;
    private Player player;
    private boolean isActive;
    private static final double BUTTON_PROPRORTION = 100d / 56d;

    private final JLabel name;
    private final JLabel points;

    private static final String PANEL_RESOURCES = "res/img/players/";

    private static final int AVATAR_PADDING = 5;
    private static final String AVATARS = "res/img/avatars/";
    private int avatarId = 0;

    public PlayerPanel(Player player) {
        this.player = player;
        isActive = false;
        setLayout(null);
        avatarId = player.getId();

        name = new JLabel(player.getName(), JLabel.LEFT);
        Font nameFont = new Font("Arial", Font.BOLD, 14);
        name.setFont(nameFont);

        points = new JLabel(String.valueOf(player.getPoints()), JLabel.RIGHT);
        Font pointsFont = new Font("Arial", Font.PLAIN, 10);
        points.setFont(pointsFont);

        add(name);
        add(points);

        addComponentListener(new ComponentAdapter() {
            @Override
            public void componentResized(ComponentEvent e) {
                resizeElements();
                loadImages();
                repaint();
            }
        });
    }

    public void setPlayer(Player player) {
        this.player = player;
    }

    private void resizeElements() {
        name.setBounds(getHeight(), 10, getWidth() - getHeight(), 15);
        points.setBounds(getHeight(), 25, getWidth() - getButtonWidth() - getHeight(), 10);
    }

    private void loadImages() {
        unactiveImage = ImagePool.getImage(getPanelResourcePath("player_panel.png"), getWidth(), getHeight());
        activeImage = ImagePool.getImage(getPanelResourcePath("player_panel_active.png"), getWidth(), getHeight());
        int avatarSize = getHeight() - 2 * AVATAR_PADDING;
        avatar = ImagePool.getImage(getAvatarResourcePath(avatarId), avatarSize, avatarSize);
    }

    private void setPoints(int pointsValue) {
        points.setText(String.valueOf(pointsValue) + " points");
    }

    private String getPanelResourcePath(String name) {
        return PANEL_RESOURCES + name;
    }

    private String getAvatarResourcePath(int avatarId) {
        return AVATARS + avatarId + ".png";
    }

    public void update(boolean isActive) {
        this.isActive = isActive;
        setPoints(player.getPoints());
    }

    public void setButton(ImageButton button) {
        if (this.button != null) {
            remove(this.button);
        }
        if (button != null) {
            this.button = button;
            add(button);
            button.setBounds(getWidth() - getButtonWidth(), 0, getButtonWidth(), getHeight());
        }
    }

    private int getButtonWidth() {
        return (int) (BUTTON_PROPRORTION * getHeight());
    }

    @Override
    public void paintComponent(Graphics g) {
        int w = getWidth();
        int h = getHeight();

        if (isActive) {
            g.drawImage(activeImage, 0, 0, w, h, null);
        } else {
            g.drawImage(unactiveImage, 0, 0, w, h, null);
        }

        g.drawImage(avatar, AVATAR_PADDING, AVATAR_PADDING, avatar.getWidth(), avatar.getHeight(), null);
    }
}
